<template>
  <div class="category-page">
    <!-- 面包屑导航 -->
    <div class="breadcrumb-container">
      <a-breadcrumb>
        <a-breadcrumb-item>
          <router-link to="/">
            <home-outlined /> 首页
          </router-link>
        </a-breadcrumb-item>
        <template v-for="(item, index) in breadcrumbs" :key="index">
          <a-breadcrumb-item>
            <router-link :to="item.path">{{ item.name }}</router-link>
          </a-breadcrumb-item>
        </template>
      </a-breadcrumb>
    </div>

    <!-- 分类标题 -->
    <div class="category-header">
      <h1 class="category-title">{{ category.name }}</h1>
    </div>

    <!-- 子菜单卡片网格 -->
    <div class="directory-grid">
      <a-card
        v-for="item in directories"
        :key="item.id"
        class="directory-card"
        :bordered="false"
        hoverable
        @click="handleDirectoryClick(item)"
      >
        <div class="directory-header">
          <folder-outlined class="directory-icon" />
          <h3 class="directory-title">{{ item.name }}</h3>
        </div>
        <div class="directory-meta">
          <span class="meta-item">
            <file-outlined /> {{ item.document_count || 0 }} 篇文档
          </span>
        </div>
      </a-card>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import {
  HomeOutlined,
  FolderOutlined,
  FileOutlined
} from '@ant-design/icons-vue'
import { categoryApi } from '@/utils/api'

const route = useRoute()
const router = useRouter()
const category = ref({})
const directories = ref([])
const breadcrumbs = ref([])

// 获取分类信息和子目录
const fetchCategory = async () => {
  try {
    const { code, data } = await categoryApi.getDetail(route.params.slug)
    if (code === 0) {
      category.value = data
      buildBreadcrumbs(data)
      fetchDirectories()
    }
  } catch (error) {
    message.error('获取分类信息失败')
    console.error(error)
  }
}

// 获取子目录
const fetchDirectories = async () => {
  try {
    const { code, data } = await categoryApi.getContents(route.params.slug)
    if (code === 0) {
      directories.value = data
    }
  } catch (error) {
    message.error('获取目录内容失败')
    console.error(error)
  }
}

// 构建面包屑
const buildBreadcrumbs = (categoryData) => {
  breadcrumbs.value = []
  if (categoryData.parent) {
    let current = categoryData.parent
    while (current) {
      breadcrumbs.value.unshift({
        name: current.name,
        path: `/category/${current.slug}`
      })
      current = current.parent
    }
  }
  breadcrumbs.value.push({
    name: categoryData.name,
    path: `/category/${categoryData.slug}`
  })
}

// 处理目录点击
const handleDirectoryClick = (item) => {
  router.push(`/category/${item.slug}`)
}

onMounted(() => {
  fetchCategory()
})
</script>

<style scoped>
.category-page {
  padding: 24px;
  max-width: 1200px;
  margin: 0 auto;
}

.breadcrumb-container {
  margin-bottom: 24px;
}

.category-header {
  margin-bottom: 24px;
}

.category-title {
  font-size: 28px;
  font-weight: 600;
  color: #1f1f1f;
  margin: 0;
}

.directory-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 20px;
}

.directory-card {
  height: 100%;
  transition: all 0.3s ease;
}

.directory-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.directory-header {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
}

.directory-icon {
  font-size: 24px;
  color: #1890ff;
}

.directory-title {
  margin: 0;
  font-size: 18px;
  font-weight: 500;
  color: #1f1f1f;
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.directory-meta {
  font-size: 13px;
  color: #999;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 4px;
}

@media (max-width: 768px) {
  .directory-grid {
    grid-template-columns: 1fr;
  }
}
</style> 